from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

from article.models import ArticlePost
from .forms import CommentForm
# 评论
from .models import Comment
# 消息通知
from notifications.signals import notify
from django.contrib.auth.models import User


# 文章评论
@login_required(login_url='/userprofile/login/')
def post_comment(request, article_id, parent_comment_id=None):
    article = get_object_or_404(ArticlePost, id=article_id)
    if request.method == 'POST':  # 处理 POST 请求
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.article = article
            new_comment.user = request.user
            if parent_comment_id:  # 二级回复
                parent_comment = Comment.objects.get(id=parent_comment_id)
                # 若回复层级超过二级，则转换为二级
                new_comment.parent_id = parent_comment.get_root().id
                new_comment.reply_to = parent_comment.user   # 被回复人
                new_comment.save()
                if not parent_comment.user.is_superuser:  # 给其他用户发送通知
                    notify.send(
                        request.user, recipient=parent_comment.user,
                        verb='回复了你', target=article, action_object=new_comment,)
                return HttpResponse('200 OK')
            new_comment.save()
            if not request.user.is_superuser:  # 给管理员发送通知
                notify.send(
                    request.user, recipient=User.objects.filter(is_superuser=1),
                    verb='回复了你', target=article, action_object=new_comment,)
            return redirect(article)
        else:
            return HttpResponse("表单内容有误，请重新填写。")
    elif request.method == 'GET':   # 处理 GET 请求
        comment_form = CommentForm()
        context = {
            'comment_form': comment_form, 'article_id': article_id,
            'parent_comment_id': parent_comment_id}
        return render(request, 'comment/reply.html', context)
    else:  # 处理其他请求
        return HttpResponse("仅接受GET/POST请求。")
